package com.owl.app;

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.owl.app.cms.util;

public class CategoryListAdapter extends BaseAdapter {
	
	LayoutInflater inflater;
	HashMap<String, String> hm;
	Context mContext;
	int mListLayout;
	private int[] listRowColor;
	public String TAG = "CategoryListAdapter";
	public int listCount = 0;
	public util cmsutil = new util();
	
	public CategoryListAdapter(Context tContext, int listLayout, 
			HashMap<String, String> hmTmp){
		mContext = tContext;
		mListLayout = listLayout;
		hm = hmTmp;
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if(hm != null){
			listCount = cmsutil.str2int(hm.get("count"));
		}
		
		listRowColor = new int[]{
				tContext.getResources().getColor(R.color.list_row_bg),
				tContext.getResources().getColor(R.color.list_row_bg_alt) };
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listCount;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return hm.get("count[" + arg0 + "]");
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView == null){
			convertView = inflater.inflate(mListLayout, parent, false); 
		}
		
		if(listRowColor != null){
			if(listRowColor.length > 0){
				int colorPos = position % listRowColor.length;
				convertView.setBackgroundColor(listRowColor[colorPos]);
			}
		}
		
		final int positionInt = position;
		
		TextView category_subject = (TextView)convertView.findViewById(R.id.category_subject);
		category_subject.setText(hm.get("subject[" + position + "]"));
		category_subject.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				categoryInfo(positionInt);
			}
		});
		
		ImageButton categoryDetailBtn = (ImageButton)convertView.findViewById(R.id.categoryDetailBtn);
		categoryDetailBtn.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				categoryInfo(positionInt);
			}
		});
		
		return convertView;
	}
	
	public void categoryInfo(int pos){
		
		String menuClass = hm.get("menuClass[" + pos + "]");
		String findClass = "";
		Class<?> cls;
		Activity actCategory;
		try{
			cls = Class.forName(menuClass);
			actCategory = (Activity)cls.newInstance();
			Intent intent = new Intent(mContext, actCategory.getClass());
			mContext.startActivity(intent);
		}catch(ClassNotFoundException e){
			findClass = "";
			AlertDialog dialog = new AlertDialog.Builder(mContext).create();
			dialog.setTitle("안내");
			dialog.setMessage("죄송합니다. \n이 버전에서는 페이지를 찾을 수 없습니다. \n 업그레이드 하세요.");
			dialog.setButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					return;
				}
			});
			dialog.show();
		}catch(InstantiationException e){
			
		}catch(IllegalAccessException e){
			
		}
	}
	
	protected class noticeDetailOKListener implements OnClickListener{
		private Dialog dialog;
		
		public noticeDetailOKListener(Dialog dialog){
			this.dialog = dialog;
		}
		public void onClick(View v){
			dialog.dismiss();
		}
	}

}
